package deltax.common.respackage.common
{
    import flash.events.*;
    import flash.utils.*;

    public class LoaderProgress extends Object
    {
        private var m_approxTotalBytes:uint = 10000000;
        private var m_curProgressStep:uint = 100;
        private var m_totalLoadedBytes:uint;
        private var m_timeAddCount:uint;
        private var m_loadingUI:ILoading;
        private var m_timer:Timer;
        private var m_text:String = "";
        private var m_delayHide:Boolean = false;
        private static var m_instance:LoaderProgress;

        public function LoaderProgress(param1:SingletonEnforcer)
        {
            this.m_timer = new Timer(5);
            this.m_timer.addEventListener(TimerEvent.TIMER, this.onTimer);
            return;
        }// end function

        public function set loadingUI(param1:ILoading) : void
        {
            this.m_loadingUI = param1;
            return;
        }// end function

        public function get loadingUICreated() : Boolean
        {
            return this.m_loadingUI != null;
        }// end function

        public function show(param1:Boolean) : void
        {
            if (!this.m_loadingUI)
            {
                return;
            }
            if (param1)
            {
                if (this.visible && this.m_timer.running)
                {
                    return;
                }
                this.m_timeAddCount = 0;
                this.m_delayHide = false;
                this.m_timer.start();
                this.m_loadingUI.showUI(true);
            }
            else
            {
                if (!this.visible)
                {
                    return;
                }
                this.m_delayHide = true;
            }
            return;
        }// end function

        public function get visible() : Boolean
        {
            return this.m_loadingUI && this.m_loadingUI.isVisible;
        }// end function

        public function set visible(param1:Boolean) : void
        {
            if (this.m_loadingUI)
            {
                this.m_loadingUI.showUI(param1);
            }
            return;
        }// end function

        public function disposeUI() : void
        {
            if (!this.m_loadingUI)
            {
                return;
            }
            this.m_loadingUI.dispose();
            this.m_loadingUI = null;
            return;
        }// end function

        private function onTimer(event:TimerEvent) : void
        {
            var _loc_4:Number = NaN;
            if (this.m_timeAddCount >= this.m_curProgressStep)
            {
                if (this.m_delayHide)
                {
                    this.m_loadingUI.showUI(false);
                    this.m_timer.stop();
                    return;
                }
                this.m_timeAddCount = 0;
            }
            else
            {
                if (this.m_delayHide)
                {
                    _loc_4 = this.m_approxTotalBytes - this.m_totalLoadedBytes;
                    this.m_totalLoadedBytes = this.m_totalLoadedBytes + _loc_4 / (this.m_curProgressStep - this.m_timeAddCount);
                }
                var _loc_5:String = this;
                var _loc_6:* = this.m_timeAddCount + 1;
                _loc_5.m_timeAddCount = _loc_6;
            }
            var _loc_2:* = this.m_totalLoadedBytes / Number(this.m_approxTotalBytes);
            var _loc_3:* = this.m_timeAddCount / Number(this.m_curProgressStep);
            this.m_loadingUI.setProgress(_loc_2 * 100, _loc_3 * 100, this.m_text);
            return;
        }// end function

        public function increaseProgress(param1:uint, param2:String = "") : void
        {
            if (!this.m_loadingUI)
            {
                return;
            }
            this.m_totalLoadedBytes = this.m_totalLoadedBytes + param1;
            if (this.m_totalLoadedBytes >= this.m_approxTotalBytes)
            {
                this.m_approxTotalBytes = this.m_totalLoadedBytes + param1;
            }
            this.m_text = param2 ? (param2) : (this.m_text);
            this.onTimer(null);
            return;
        }// end function

        public static function get instance() : LoaderProgress
        {
            var _loc_1:* = m_instance || new LoaderProgress(new SingletonEnforcer());
            m_instance = m_instance || new LoaderProgress(new SingletonEnforcer());
            return _loc_1;
        }// end function

    }
}

class SingletonEnforcer extends Object
{

    function SingletonEnforcer()
    {
        return;
    }// end function

}

